GJ.add('app/dingdong/dingdong_widget.js', ['jquery', 'app/common/widget/widget.js'], function(require, exports) {
    var $ = require('jquery');

    exports.css3Animate = function() {
        var tops = [];
        var i = 0;

        $('.section').each(function() {
            tops.push({
                'top': $(this).offset().top,
                '$el': $(this)
            });
        });

        $(window).on('scroll', function() {
            if (i >= tops.length) {
                $(window).off('scroll');
                return;
            }

            var top = $(window).scrollTop();
            while (tops[i] && top + 150 > tops[i].top) {
                tops[i].$el.addClass('active');
                i++;
            }
        });
        $(window).trigger('scroll');
    };

    exports.showOrHide = function(config) {
        var $trigger = config.$trigger;
        var st;

        $trigger.hover(function() {
            var $target = $('.' + $(this).data('target'));
            $target.show();
            $target.on('mouseenter', function(){
                clearTimeout(st);
            }).on('mouseleave', function(){
                $(this).hide();
            });
        }, function() {
            var $target = $('.' + $(this).data('target'));
            st=setTimeout(function(){
                $target.hide();
            }, 100);
        });
    };

    exports.mySlide = function(config) {
        var $el = config.$el;
        var $item = $el.find('.box-right.fr');
        var loop;
        $el.on('mouseenter', '[data-slide-to]', function(e) {
            var index = $(e.target).data('slideTo');
            if ($(this).hasClass('active')) {
                return;
            }
            slideTo(index);
        });

        function slideTo(index) {
            var $pointers = $('[data-slide-to]');
            $pointers.filter('.active').removeClass('active');
            $pointers.eq(index).addClass('active');
            $item.filter('.show').removeClass('show').addClass('hide').hide();
            $el.find('.box-right.fr').eq(index).removeClass('hide').addClass('show').show();
        }

        loop = setInterval(function() {
            var index = $('[data-slide-to]').filter('.active').index();
            index++;
            if(index>=$item.length){
                index = 0;
            }
            slideTo(index);
        }, 3000);

        $el.hover(function() {
            clearInterval(loop);
        }, function() {
            clearInterval(loop);
            loop = setInterval(function() {
                var index = $('[data-slide-to]').filter('.active').index();
                index++;
                if(index>=$item.length){
                    index = 0;
                }
                slideTo(index);
            }, 3000);
        });
    };

    exports.scrollAnimate = function(config) {
        var itemPos = [];
        var itemHeight = config.height;

        config.$item.each(function() {
            itemPos.push({
                $item: $(this),
                top: $(this).offset().top
            });
        });


        var timer = null;

        var scrollFn = function() {
            clearTimeout(timer);
            timer = setTimeout(function() {
                var curTop = $(window).scrollTop();
                if (config.$item.filter('.active').length >= itemPos.length) {
                    $(window).off('scroll');
                    return;
                }

                for (var i = itemPos.length - 1; i >= 0; i--) {
                    if(curTop + itemHeight * 0.6 > itemPos[i].top && curTop < itemPos[i].top + itemHeight) {
                        itemPos[i].$item.addClass('active');
                    }
                }
            }, 0);
        };

        $(window).on('scroll', scrollFn);

        scrollFn();
    };

    var dingdong_cfg = {
        latestVersion : 3.4,
        stableVersion : 3.4,
        latestUrl : 'http://appdl.ganjistatic1.com/ganji/dingdong/pc/setup/dingdong_3.4.6_full.exe',
        stableUrl : 'http://appdl.ganjistatic1.com/ganji/dingdong/pc/setup/dingdong_3.4.6_full.exe',
        latestFileSize : '29.9M'
    };

    //新版微聊的下载页面
    var weiliao_cfg = {
        latestVersion : 1.0,
        stableVersion : 1.0,
        latestUrl : '//weiliao.58.com/',
        stableUrl : '//weiliao.58.com/',
        latestFileSize : '18.1M'
    };

    //老版叮咚跳转到新版微聊
    dingdong_cfg = weiliao_cfg;

    exports.setStableLink = function(config){
        var $el = config.$el;
        $el.attr('href', dingdong_cfg.stableUrl);
        $el.text('稳定版' + dingdong_cfg.stableVersion + '下载'); 
    };

    exports.setLatestLink = function(config){
        var $el = config.$el;
        $el.attr('href', dingdong_cfg.latestUrl);
    };

    exports.setLatestFileVersion = function(config){
        var $el = config.$el;
        $el.text($.trim($el.text()) + dingdong_cfg.latestVersion + 'beta');
    };

    exports.setLatestFileSize = function(config){
        var $el = config.$el;
        $el.text($.trim($el.text()) + dingdong_cfg.latestFileSize);
    };
});
